<?php
	require_once("db/database.php");

	class Random_AI{

		public function deployment($session_key, $continents) {
			$continent_array = array();
			$i = 0;	
			foreach($continents as $key => $continent) {
				foreach($continent as $toponym) {
					$continent_array[$i] = $toponym;
					$i++;
				}
			}
			do {
				$rand_num = mt_rand(0, $i-1);
				$result = db_query("SELECT owner FROM map_status WHERE toponym = '".$continent_array[$rand_num]."'");
				$row = db_fetch_array($result);
			} while ($row["owner"] != "");
			return $continent_array[$rand_num];
		}
		
		public function post_deployment($session_key) {
			$possible_toponym_array = array();
			
			$result = db_query("SELECT toponym FROM map_status WHERE owner = $session_key");
			$i = 0;
			while($row = db_fetch_array($result)) {
				$possible_toponym_array[$i] = $row["toponym"];
				$i++;
			}
			
			$rand_num = mt_rand(0, $i-1);
			return $possible_toponym_array[$rand_num];
		}
		
		public function engagement_planning($session_key, $continents, $no_of_refill) {
			$chosen_land_array = array();
			for ($i = 0; $i < $no_of_refill; $i++) {
				$tmp = $this->post_deployment($session_key, $continents);
				$chosen_land_array[$i] = $tmp;
			}
			
			return $chosen_land_array;
		}
		
		public function engagement_attack($session_key) {
			$possible_source_array = array();
			$possible_target_array = array();
			
			$result = db_query("SELECT toponym FROM map_status WHERE owner = $session_key AND troops > 1");
			$i = 0;
			while($row = db_fetch_array($result)) {
				$possible_source_array[$i] = $row["toponym"];
				$i++;
			}
			
			if (count($possible_source_array) == 0) {
				return "No";	
			}
			
			$rand_num = mt_rand(0, $i - 1);
			$source = $possible_source_array[$rand_num];
		
			$neighbour_array = $this->get_neighbour_toponyms($source);
			// Check can fight
			foreach ($neighbour_array as $element) {
				$result = db_query("SELECT toponym FROM map_status WHERE toponym = '$element' AND troops > 0 AND owner != $session_key");
				$row = db_fetch_array($result);
				if ($row["toponym"] != "") {
					$idx = count($possible_target_array);
					$possible_target_array[$idx] = $row["toponym"];
				}
			}
			
			if (count($possible_target_array) == 0) {
				return "No";
			}
			
			$rand_num = mt_rand(0, count($possible_target_array) - 1);
			$target = $possible_target_array[$rand_num];
			return $source . "," . $target;
		}
		
		private function get_neighbour_toponyms($source, $link_info_file = "./map/default_small/link_info.txt") {
			$source = str_replace("_", " ", $source);
			$result_array = array();
			$fh = fopen($link_info_file, 'r');

			while (!feof($fh)) {
				$line = fgets($fh);
				if (strpos($line, stristr(trim($line), trim($source))) === 0) {
					$token = strtok($line, ",>");
					$count = 0;
					while ($token !== false) {
						if ($count > 0) {	// Skip the first one(source)
							$result_array[$count - 1] = str_replace(" ", "_", trim($token));
						}
						$token = strtok(",>");
						$count++;
					}
					break;
				}
			}
			fclose($fh);
			return $result_array;
		}
	}
	

	
?>